package cn.wolfcode.luowowo.website.controller;


import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    @Reference
    private IUserInfoService userInfoService;

    //前端点击立即注册 判断手机号是否存在
    @RequestMapping("checkPhone")
    @ResponseBody
    public Object checkPhone(String phone) {
        return !userInfoService.checkPhone(phone);
    }

    //发送短信
    @RequestMapping("sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone) {
        userInfoService.sendVerifyCode(phone);
        return AjaxResult.SUCCESS;
    }

    //注册
    @RequestMapping("userRegist")
    @ResponseBody
    public Object userRegist(String nickname, String password, String rpassword, String verifyCode, String phone) {
        userInfoService.userRegist(nickname, password, rpassword, verifyCode, phone);
        return AjaxResult.SUCCESS;

    }


    //登录
    @RequestMapping("userLogin")
    @ResponseBody
    public Object userLogin(String username, String password, HttpServletResponse response) {
        //登录
        String token = userInfoService.userLogin(username, password);

        //把token（redis的key）共享到浏览器
        CookieUtil.addCookie(response,"token",token,RedisKeys.USER_LOGIN_TOKEN.getTime().intValue());

        return AjaxResult.SUCCESS;
    }


}
